package vn.orderonline.core.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import vn.orderonline.core.commons.utils.StringUtils;
import vn.orderonline.core.dao.common.GenericDaoImpl;
import vn.orderonline.core.dao.entity.Drink;

@Repository
public class DrinkDaoImpl extends GenericDaoImpl<Drink, Integer> implements
		DrinkDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getListDrinkByResIdAndMenuDrinkId(Integer resId,
			Integer menuDrinkId) {
		String hql = "Select res.restaurantId, res.restaurantName, md.menuDrinkId, md.menuDrinkName, d "
				+ "From Restaurant res, RestaurantMenuDrink rmd, MenuDrink md, MenuDrinkDrink mdd, Drink d "
				+ "Where res.restaurantId = rmd.id.restaurantId And rmd.id.menuDrinkId = md.menuDrinkId And "
				+ "md.menuDrinkId = mdd.id.menuDrinkId And mdd.id.drinkId = d.drinkId And "
				+ "res.restaurantId = :restaurantId And md.menuDrinkId = :menuDrinkId";
		Query query = getSession().createQuery(hql);
		query.setParameter("restaurantId", resId);
		query.setParameter("menuDrinkId", menuDrinkId);
		return query.list();
	}

	@Override
	public Boolean checkExistedDrink(Integer menuDrinkId, String drinkName) {
		String hql = "From MenuDrink md, MenuDrinkDrink mdd, Drink d "
				+ "Where md.menuDrinkId = mdd.id.menuDrinkId And mdd.id.drinkId = d.drinkId "
				+ "And md.menuDrinkId = :menuDrinkId And d.drinkName = :drinkName";
		Query query = getSession().createQuery(hql);
		query.setParameter("menuDrinkId", menuDrinkId);
		query.setParameter("drinkName", drinkName);
		return (query.uniqueResult() != null) ? true : false;
	}

	@Override
	public Object[] getDrink(Integer drinkId) {
		String hql = "Select res.restaurantId, res.restaurantName, md.menuDrinkId, md.menuDrinkName, d "
				+ "From Restaurant res, RestaurantMenuDrink rmd, MenuDrink md, MenuDrinkDrink mdd, Drink d "
				+ "Where res.restaurantId = rmd.id.restaurantId And rmd.id.menuDrinkId = md.menuDrinkId And "
				+ "md.menuDrinkId = mdd.id.menuDrinkId And mdd.id.drinkId = d.drinkId And d.drinkId = :drinkId";
		Query query = getSession().createQuery(hql);
		query.setParameter("drinkId", drinkId);
		return (Object[]) query.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> searchDrink(Integer resId, Integer menuDrinkId,
			Integer drinkId, String drinkName, String drinkTitle,
			Float oldPrice, Float newPrice) {
		String hql = "Select res.restaurantId, res.restaurantName, md.menuDrinkId, md.menuDrinkName, d "
				+ "From Restaurant res, RestaurantMenuDrink rmd, MenuDrink md, MenuDrinkDrink mdd, Drink d "
				+ "Where res.restaurantId = rmd.id.restaurantId And rmd.id.menuDrinkId = md.menuDrinkId And "
				+ "md.menuDrinkId = mdd.id.menuDrinkId And mdd.id.drinkId = d.drinkId ";
		StringBuilder str = new StringBuilder(hql);
		if (!StringUtils.isNullOrEmpty(drinkName)) {
			str.append("And d.drinkName like :drinkName ");
		}
		if (!StringUtils.isNullOrEmpty(drinkTitle)) {
			str.append("And d.drinkTitle like :drinkTitle ");
		}
		if (oldPrice != null) {
			str.append("And d.oldPrice = :oldPrice ");
		}
		if (newPrice != null) {
			str.append("d.newPrice = :newPrice");
		}
		Query query = getSession().createQuery(str.toString());
		if (!StringUtils.isNullOrEmpty(drinkName)) {
			query.setParameter("drinkName", "%" + drinkName + "%");
		}
		if (!StringUtils.isNullOrEmpty(drinkTitle)) {
			query.setParameter("drinkTitle", "%" + drinkTitle + "%");
		}
		if (oldPrice != null) {
			query.setParameter("oldPrice", oldPrice);
		}
		if (newPrice != null) {
			query.setParameter("newPrice", newPrice);
		}
		return query.list();
	}

	@Override
	public String getDrinkNameByDrinkId(Integer drinkId) {
		Criteria criteria = getSession().createCriteria(Drink.class);
		criteria.setProjection(Projections.property("drinkName"));
		criteria.add(Restrictions.eq("drinkId", drinkId));
		return (String) criteria.uniqueResult();
	}

}
